home *** CD-ROM | disk | FTP | other *** search
/ JCSM Shareware Collection 1996 September / JCSM Shareware Collection (JCS Distribution) (September 1996).ISO / bother__ / cenvid.zip / CENVIDOS.ZIP / WINCLIP.CMM < prev    next >
Text File  |  1994-10-06  |  7KB  |  222 lines

  1. //***************************************************
  2. //*** WinClip - Command-line access to Windows    ***
  3. //*** ver.2     clipboard text functions from DOS ***
  4. //***************************************************
  5.  
  6. #include <WinClip.lib>
  7.  
  8. main(argc,argv)
  9. {
  10.    Cmd = argv[1];
  11.    Text = argv[2];
  12.    success = False;  // assume failure
  13.    if ( argc == 2 ) {
  14.       if      ( !stricmp(Cmd,"DELETE") )     success = DeleteClipBrd();
  15.       else if ( !stricmp(Cmd,"GET") )        success = GetClipBrd();
  16.       else if ( !stricmp(Cmd,"ISORT") )      success = SortClipBrd(False);
  17.       else if ( !stricmp(Cmd,"LOWER") )      success = LowercaseClipBrd();
  18.       else if ( !stricmp(Cmd,"QUERY") )      success = QueryClipBrd();
  19.       else if ( !stricmp(Cmd,"SORT") )       success = SortClipBrd(True);
  20.       else if ( !stricmp(Cmd,"TEXT") )       success = TextOnlyInClipBrd();
  21.       else if ( !stricmp(Cmd,"UPPER") )      success = UppercaseClipBrd();
  22.       else Instructions();
  23.    } else if ( argc == 3 ) {
  24.       if ( !stricmp(Cmd,"GET") && Text[0] == '@' )
  25.          success = GetClipBrd(Text+1);
  26.       else {
  27.          if ( Text[0] == '@' ) {
  28.             // Get text from FileSpec
  29.             Text = ReadTextFromFile(Text+1);
  30.          }
  31.          if      ( !stricmp(Cmd,"APPEND") )     success = AppendToClipBrd(Text);
  32.          else if ( !stricmp(Cmd,"FIND") )       success = FindInClipboard(Text,True);
  33.          else if ( !stricmp(Cmd,"IFIND") )      success = FindInClipboard(Text,False);
  34.          else if ( !stricmp(Cmd,"PREPEND") )    success = PrependToClipBrd(Text);
  35.          else if ( !stricmp(Cmd,"PUT") )        success = PutInClipBrd(Text);
  36.          else Instructions();
  37.       }
  38.    } else
  39.       Instructions();
  40.    return ( success ? EXIT_SUCCESS : EXIT_FAILURE );
  41. }
  42.  
  43. Instructions()
  44. {
  45.    puts("\a")
  46.    puts("SYNTAX:")
  47.    puts("   CEnviD WinClip [DELETE | GET | ISORT | LOWER | QUERY | SORT | TEXT | UPPER]")
  48.    puts("   CEnviD WinClip [ APPEND | FIND | IFIND | PREPEND | PUT ] <text | @<file>>")
  49.    puts("   CEnviD WinClip GET @<file>")
  50.    puts("")
  51.    puts("Where: APPEND - Append new text to end of existing clipboard")
  52.    puts("       DELETE - Delete contents of the clipboard")
  53.    puts("       FIND - Find case-sensitive string in clipboard")
  54.    puts("       GET - Display contents of clipboard; or write to @<file>")
  55.    puts("       IFIND - Find case-insensitive string in clipboard")
  56.    puts("       ISORT - Case-insenstive, alphabetic sort of clipboard contents")
  57.    puts("       PREPEND - Prepend new text to beginning of existing clipboard")
  58.    puts("       PUT - Put text in clipboard")
  59.    puts("       QUERY - Does clipboard contain text?")
  60.    puts("       SORT - Case-sensitive, alphabetic sort of clipboard contents")
  61.    puts("       TEXT - Remove all clipboard types except for text")
  62.    puts("       @<FILE> - file specification to get text from/copy-to")
  63.    puts("")
  64.    printf("Return: Return ERRORLEVEL %d if success, and %d if error. QUERY, FIND, and\n",EXIT_SUCCESS,EXIT_FAILURE)
  65.    printf("        IFIND return %d if found, and %d if not found\n",EXIT_SUCCESS,EXIT_FAILURE)
  66.    puts("");
  67.    puts(`Examples: CEnviD WinClip DELETE                   Delete clipboard contents`)
  68.    puts(`          CEnviD WinClip GET @CLIP.TXT            Save to file CLIP.TXT`)
  69.    puts(`          CEnviD WinClip Append "Little bunny."   Add text to clipboard`)
  70.    puts(`          CEnviD WinClip GET PRN:                 Send clipboard to printer`)
  71. }
  72.  
  73. ReadTextFromFile(pFileSpec)   // return string; else exit
  74. {
  75.    if ( !(fp = fopen(pFileSpec,"rb")) ) {
  76.       printf("Unable to open file \%s\" for reading.\n",pFileSpec);
  77.       exit(EXIT_FAILURE);
  78.    }
  79.    // read in file CHUNK_SIZE bytes at a time
  80.    #define CHUNK_SIZE 1000
  81.    lText = "", lTextLen = 0;
  82.    do {
  83.       lRead = fread(lChunk,CHUNK_SIZE,fp);
  84.       memcpy(lText+lTextLen,lChunk,lRead);
  85.       lTextLen += lRead;
  86.    } while (CHUNK_SIZE == lRead);
  87.    fclose(fp);
  88.    lText[lTextLen] = '\0';
  89.    return lText;
  90. }
  91.  
  92. AppendToClipBrd(pText)
  93. {
  94.    if ( !(data = GetClipboardText()) )
  95.       data = "";
  96.    strcat(data,pText);
  97.    return PutClipboardText(data);
  98. }
  99.  
  100. DeleteClipBrd()
  101. {
  102.    return ( PutClipboardText(NULL) );
  103. }
  104.  
  105. FindInClipboard(pText,pCaseSensitive)
  106. {
  107.    lTextLen = strlen(pText);
  108.    if ( pCaseSensitive ) {
  109.       lFindChars[0] = pText[0], lFindChars[1] = '\0';
  110.       lFindFunction = "memcmp";
  111.    } else {
  112.       lFindChars[2] = '\0';
  113.       lFindChars[1] = tolower(pText[0]);
  114.       lFindChars[0] = toupper(pText[0]);
  115.       lFindFunction = "memicmp";
  116.    }
  117.    if ( data = GetClipboardText() ) {
  118.       while ( data = strpbrk(data,lFindChars) ) {
  119.          if ( !function(lFindFunction,data,pText,lTextLen) ) {
  120.             printf("Text found.\n");
  121.             return True;
  122.          }
  123.          data++;
  124.       }
  125.    }
  126.    printf("Text not found.\n");
  127.    return False;
  128. }
  129.  
  130. GetClipBrd(OptionalFileSpec)
  131. {
  132.    if ( data = GetClipboardText() ) {
  133.       if ( va_arg() ) {
  134.          if ( !(fp = fopen(OptionalFileSpec,"wb")) ) {
  135.             printf("Error opening file \"%s\" for writing.\n",OptionalFileSpec);
  136.             data = NULL;
  137.          } else {
  138.             fwrite(data,strlen(data),fp);
  139.             fclose(fp);
  140.          }
  141.       } else
  142.          fwrite(data,strlen(data),stdout);
  143.    }
  144.    return(data != NULL);
  145. }
  146.  
  147. LowercaseClipBrd()
  148. {
  149.    if ( data = GetClipboardText() )
  150.       strlwr(data);
  151.    return( data != NULL && PutClipboardText(data));
  152. }
  153.  
  154. PrependToClipBrd(pText)
  155. {
  156.    if ( !(data = GetClipboardText()) )
  157.       data = "";
  158.    strcat(pText,data);
  159.    return PutClipboardText(pText);
  160. }
  161.  
  162. PutInClipBrd(pText)
  163. {
  164.    return PutClipboardText(pText);
  165. }
  166.  
  167. QueryClipBrd()
  168. {
  169.    return ( NULL != GetClipboardText() );
  170. }
  171.  
  172. SortClipBrd(CaseSensitive)
  173. {
  174.    if ( data = GetClipboardText() ) {
  175.  
  176.       // determine if the last character is line-feed, so we'll
  177.       // know whether to add line-feed in the sorted list
  178.       AddFinalLineFeed = ( NULL != strchr("\r\n",data[strlen(data)-1]) );
  179.  
  180.       // build array of each line of text in the clipboard
  181.       LineCount = 0;
  182.       Line = strtok(data,"\r\n");
  183.       while ( Line ) {
  184.          strcpy(List[LineCount++],Line);
  185.          Line = strtok(NULL,"\r\n");
  186.       }
  187.  
  188.       // sort this array of text alphabetically, case-(in)sensitive
  189.       qsort(List,LineCount,CaseSensitive ? "strcmp" : "stricmp" );
  190.  
  191.       // build one long string to put data back into the clipboard
  192.       data[0] = '\0';
  193.       for ( i = 0; i < LineCount; i++ ) {
  194.          strcat(data,List[i]);
  195.          strcat(data,"\r\n");
  196.       }
  197.  
  198.       // if there wasn't supposed to be a line-feed at the end, then remove it
  199.       if ( !AddFinalLineFeed )
  200.          data[strlen(data)-2] = '\0';
  201.  
  202.       // put this new data string in clipboard, replacing old
  203.       if ( !PutClipboardText(data) )
  204.          data = NULL;
  205.    }
  206.    return(data != NULL);
  207. }
  208.  
  209. TextOnlyInClipBrd()
  210. {
  211.    return ( NULL != (data=GetClipboardText())
  212.          && PutClipboardText(data) );
  213. }
  214.  
  215. UppercaseClipBrd()
  216. {
  217.    if ( data = GetClipboardText() )
  218.       strupr(data);
  219.    return( data != NULL && PutClipboardText(data));
  220. }
  221.  
  222.